var _eiinfo;
var _regions=new Array() 
_regions[0]="ef_region_inqu";
_regions[1]="ef_region_result";
_regions[2]="ef_region_inqu1"; 
_regions[3]="ef_region_detail";
_regions[4]="ef_region_inqu2"; 

efform_onload = function ()
{
	_eiinfo=new _getEi();
	$('#ef_region_inqu1').hide();
	$('#ef_region_inqu2').hide();
	hideAndShowSubButtons(false);
	queryMain();
}; 
function queryMain()
{
	efgrid.submitInqu( "ef_grid_r", "ZM","ZMCP01","query");
}
button_query_onclick = function () 
{   	
	queryMain();
}
button_update_onclick = function (){ 
	var grid=isSelectedOne("ef_grid_r");
	if (grid == 0)return; 
//	if(grid.getCellValue(grid.getCheckedRows(), 0, TYPE_DATA)!="草拟")
//	{
//		EFAlert("只能对草拟的记录进行修改！"); return;
//	}
	var selectedRow=grid.getSelectRowsData()[0];
	for(var item in selectedRow){
		var colname=item;
		var colvalue=selectedRow[item];
		$('#ef_region_inqu1').find('#add_status-0-'+colname).val(colvalue);
	} 
	$('#ef_region_inqu1').find('#add_status-0-fdPlanYear').attr("readonly","readonly");
	$('#ef_region_inqu1').find('#add_status-0-fdPlanMonth').attr("disabled","disabled");
	//隐藏/显示相关region  
	hideAllRegions(_regions);
	$('#ef_region_inqu1').show();
}
button_insert_onclick = function (){
	var date=new Date;
	//取消只读的字段
	$('#ef_region_inqu1').find('#add_status-0-fdPlanYear').removeAttr("readonly");
	$('#ef_region_inqu1').find('#add_status-0-fdPlanMonth').removeAttr("disabled");
	//将所有input清空
    $('#ef_region_inqu1').find("input").each(function (){$(this).val('');})
    //填充默认值
	$('#ef_region_inqu1').find('#add_status-0-fdPlanSum').val(0);
	$('#ef_region_inqu1').find('#add_status-0-fdPlanStatus').val("草拟");
	$('#ef_region_inqu1').find('#add_status-0-fdPlanYear').val(date.getFullYear());
	$('#ef_region_inqu1').find('#add_status-0-fdPlanMonth').val(date.getMonth()+1);
	$('#ef_region_inqu1').find('#add_status-0-fdEditDate').val(date.toLocaleDateString());
	$('#ef_region_inqu1').find('#add_status-0-fdCreateDepartmentId').val(_eiinfo.get('fdCreateDepartmentId'));
	$('#ef_region_inqu1').find('#add_status-0-fdCreateDepartment').val(_eiinfo.get('fdCreateDepartment'));
	$('#ef_region_inqu1').find('#add_status-0-fdCreatorId').val(_eiinfo.get('fdCreatorId'));
	$('#ef_region_inqu1').find('#add_status-0-fdCreator').val(_eiinfo.get('fdCreator'));
	//隐藏/显示相关region
	hideAllRegions(_regions);
	$('#ef_region_inqu1').show();
}
button_save_onclick = function (){ 
	if(efvalidateDiv("ef_region_inqu1")){
		var info = getDetail("ef_region_inqu1","add_status");
		if (null != info) {
			if($('#ef_region_inqu1').find('#add_status-0-fdKeyId').val())
			{
				EiCommunicator.send("ZMCP01", "update", info, ajax_callback);
			}else
			{
				EiCommunicator.send("ZMCP01", "insert", info, ajax_callback);
			}
		} 
	}
}
var ajax_callback= {
	onSuccess: function(eiInfo) {
		hideAllRegions(_regions)
		$('#ef_region_inqu').show();
		$('#ef_region_result').show();
		$('#ef_region_detail').show();
		queryMain(); 
	},
	onFail:function(eMsg)
	{
		EFAlert("新增失败，原因："+eMsg); 
	}
}
button_cancel_onclick = function (){
	hideAllRegions(_regions);
	$('#ef_region_inqu').show();
	$('#ef_region_result').show();
	$('#ef_region_detail').show();
}
button_delete_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("只能对草拟的记录进行删除！"); return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		efgrid.submitForm( "ef_grid_r", "ZM","ZMCP01","delete",true);
	});
	
}
button_print_onclick = function (){ 
	alert("打印");
}
button_report_onclick = function (){ 
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")=="上报")
		{
			EFAlert("已经上报的记录，不能再次上报！"); return;
		}
	}
	EFConfirm('你确认要上报吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM","ZMCP01","report",true);
	});
}
efgrid_onRowClicked = function( grid_id, row_index ){
	if(grid_id == "ef_grid_r"){ 
		var grid = efgrid.getGridObject(grid_id);
		var fdProfessionalplanId=grid.getCellValue(row_index, 0, TYPE_DATA, true);
		var fdPlanYear=grid.getCellValue(row_index, 1, TYPE_DATA, false);
		var fdPlanMonth=grid.getCellValue(row_index, 2, TYPE_DATA, false);
		var fdPlanStatus=grid.getCellValue(row_index, 0, TYPE_DATA, false);
		//给从表新增region赋值
		$('#ef_region_inqu2').find('#det_status-0-fdProfessionalplanId').val(fdProfessionalplanId);
		$('#ef_region_inqu2').find('#det_status-0-fdPlanYear').val(fdPlanYear);
		$('#ef_region_inqu2').find('#det_status-0-fdPlanMonth').val(fdPlanMonth);
		//隐藏/显示从表按钮
		if(fdPlanStatus!='草拟')
		{
			hideAndShowSubButtons(false);
		}else
		{
			hideAndShowSubButtons(true);
		}
		var info = new EiInfo();
		info.set("fdProfessionalplanId",fdProfessionalplanId); 
		info.set("fdPlanYear",fdPlanYear); 
		info.set("fdPlanMonth",fdPlanMonth); 
		info.set("fdPlanStatus",fdPlanStatus); 
		EiCommunicator.send("ZMCP02", "query", info, ajax_chang_callback);  
	}
}
//efgrid_onRowCheckboxClicked = function( grid_id, row_index,div_node )
//{
//	if(grid_id == "ef_grid_r"){ 
//		var grid = efgrid.getGridObject(grid_id);
//		var info = new EiInfo();
//		info.set("fdProfessionalplanId",grid.getCellValue(row_index, 0, TYPE_DATA, true)); 
//		info.set("fdPlanYear",grid.getCellValue(row_index, 1, TYPE_DATA, false)); 
//		info.set("fdPlanMonth",grid.getCellValue(row_index, 2, TYPE_DATA, false)); 
//		info.set("fdPlanStatus",grid.getCellValue(row_index, 0, TYPE_DATA, false)); 
//		EiCommunicator.send("ZMCP02", "query", info, ajax_chang_callback); 
//	}
//} 
var ajax_chang_callback= {
	onSuccess: function(eiInfo) { 
		 efwindow.hide();
		 var grid = efgrid.getGridObject("ef_grid_d");
		 grid.refresh(eiInfo);
	},
	onFail:function(eMsg)
	{
		EFAlert("新增失败，原因："+eMsg); 
	}
}

/********************************************************************************
 ***********************************从表操作**************************************** 
 ********************************************************************************/
button_insert1_onclick = function (){
	
	//将所有input清空
    $('#ef_region_inqu2').find("input").each(function ()
    {
    	var id=$(this).attr("id");
    	if(!(id.indexOf("fdProfessionalplanId")>0 || id.indexOf("fdPlanYear")>0 || id.indexOf("fdPlanMonth")>0))
    		$(this).val('');
    })
	hideAllRegions(_regions)
	$('#ef_region_inqu2').show();
}
button_update1_onclick = function (){
	var grid=isSelectedOne("ef_grid_d");
	if (grid == 0)return; 
	var selectedRow=grid.getSelectRowsData()[0];
	for(var item in selectedRow){
		var colname=item;
		var colvalue=selectedRow[item];
		$('#ef_region_inqu2').find('#det_status-0-'+colname).val(colvalue);
	} 
	//隐藏/显示相关region  
	hideAllRegions(_regions);
	$('#ef_region_inqu2').show();
}
button_delete1_onclick = function (){
	var grid=isSelected("ef_grid_d");
	if (grid == 0)return;  
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r){
			efgrid.submitForm( "ef_grid_d", "ZM","ZMCP02","delete",true);
		}
	});
	
}
button_save1_onclick = function (){ 
	if(efvalidateDiv("ef_region_inqu2")){
		var info = getDetail("ef_region_inqu2","det_status");
		if (null != info) {
			if($('#ef_region_inqu2').find('#det_status-0-fdKeyId').val())
			{
				EiCommunicator.send("ZMCP02", "update", info, ajax_callback1);
			}else
			{
				EiCommunicator.send("ZMCP02", "insert", info, ajax_callback1);
			}
		} 
	}

}
var ajax_callback1= {
	onSuccess: function(eiInfo) {
		hideAllRegions(_regions)
		$('#ef_region_inqu').show();
		$('#ef_region_result').show();
		$('#ef_region_detail').show();
		var info = new EiInfo();
		info.set("fdProfessionalplanId",$('#ef_region_inqu2').find('#det_status-0-fdProfessionalplanId').val()); 
		info.set("fdPlanYear",$('#ef_region_inqu2').find('#det_status-0-fdPlanYear').val()); 
		info.set("fdPlanMonth",$('#ef_region_inqu2').find('#det_status-0-fdPlanMonth').val()); 
		EiCommunicator.send("ZMCP02", "query", info, ajax_chang_callback);  
	},
	onFail:function(eMsg)
	{
		EFAlert("新增失败，原因："+eMsg); 
	}
}
button_cancel1_onclick = function (){
	hideAllRegions(_regions);
	$('#ef_region_inqu').show();
	$('#ef_region_result').show();
	$('#ef_region_detail').show();
}
var hideAndShowSubButtons=function(status)
{
	if(!status)
	{
		efbutton.setButtonStatus("insert1", false);
		efbutton.setButtonStatus("update1", false);
		efbutton.setButtonStatus("delete1", false);
	}else
	{
		efbutton.setButtonStatus("insert1", true);
		efbutton.setButtonStatus("update1", true);
		efbutton.setButtonStatus("delete1", true);
	}
}